// pages/customVaccine/customVaccine.js
const util = require('../../utils/util.js')
import * as utils from '../../utils/util.js'
import request from '../../utils/http.js'
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
  * 页面的初始数据
  */
  data: {
    showDate:false,    //接种日期弹框
    show: false,    //接种状态弹框
    actions: [{
      name: '未接种',
      status: 0
    },
    {
      name: '已接种',
      status: 1
    },
    ],
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      } else if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    showType: false,
    actionsType: [{
      name: '一类(免费)'
    }, {
      name: '二类(自费)'
    }],
    currentDate: new Date().getTime(),
    name:'',
    nameError:'',
    statusName:'未接种',
    status:0,
    planDate: '',
    node:'',
    nodeError:'',
    babyId:'',
    type:'一类(免费)'
  },

  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    this.data.babyId=options.babyId
  },
  /**
  * 生命周期函数--监听页面显示
  */
  onShow: function () {
    this.setData({
      planDate: utils.formatDate(new Date())
    })
  },
  submit(){
    if(this.data.node==''){
      this.setData({
        nodeError: '请输入接种备注'
      })
      return;
    }
    if(this.data.nodeError==''&&this.data.nameError==''){
      let data = {
        babyId: this.data.babyId,
        name:this.data.name,
        status: this.data.status,
        planTime: this.data.planDate,
        description:this.data.node,
        type: this.data.type
      }
      utils.HttpRequst(false, '/baby-cloud-base/mobileVaccinePlan/addItem', 'post', { ...data }, res => {
        if(res.data.code==200){
          Toast("添加成功")
          wx.navigateBack()
        }else{
          Toast(res.data.message)
        }
      })
      console.log("提交")
    }
    
  },
  openType() {
    this.setData({
      showType: true
    })
  },
  //关闭疫苗类别弹框
  closeType() {
    this.setData({
      showType: false
    })
  },
  //选择疫苗类别
  selectType(e) {
    console.log(e.detail.name)
    this.setData({
      type: e.detail.name
    })
  },
  //打开日期选择器
  openDate() {
    this.setData({
      showDate: true
    })
  },
  onDateClose(){
    this.setData({
      showDate: false
    })
  },
  //修改接种日期
  onConfirm(event) {
    console.log(utils.formatDate(new Date(event.detail)))
    let planDate = utils.formatDate(new Date(event.detail))
    this.setData({
      planDate: planDate
    })
    this.onDateClose()
  },
  //修改接种状态
  onSelect(e) {
    console.log(e)
    this.setData({
      status: e.detail.status,
      statusName: e.detail.name,
    })
  },
  //关闭状态选择器
  onClose() {
    this.setData({
      show: false
    })
  }, 

  //打开状态选择器
  openStatus(){
    this.setData({
      show: true
    })
  },
  //失去焦点时校验疫苗名称
  checkName(e){
    let name = e.detail.value
    if(name==''){
      this.setData({
        nameError: '请输入疫苗名称'
      })
    }
  },
  //输入时限制用户名20个字符
  checkInputName(e){
    let text = e.detail
    let len=text.length
    if (len<21){
      this.setData({
        name: text,
        nameError: ''
      })
    }else{
      this.setData({
        nameError: '疫苗名称字数不能超过20个字符'
      })
    }
  },
   //失去焦点时校验接种备注
  checkNode(e){
    let node = e.detail.value
    if (node == '') {
      this.setData({
        nodeError: '请输入接种备注'
      })
    }
  },
  //限制接种备注数字500
  checkInputNode(e) {
    let text = e.detail
    let len = text.length
    if (len < 501) {
      this.setData({
        node: text,
        nodeError: ''
      })
    } else {
      this.setData({
        nodeError: '接种备注字数不能超过500个字符'
      })
    }
  },
  // 限制输入框内字数
  // bindText: function (e) {
  //   var t_text = e.detail.value.length;
  //   console.log(t_text)
  //   this.setData({
  //     t_length: t_text
  //   })
  // },
})